// 增删改查通用mixin
import Backstage from '../services/backstage'
export default {
  data() {
    return {
      // 分页数据
      pageData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      // 表头查询重置按钮
      btnList: [
        {
          type: 'primary',
          text: '查询',
          click: () => {
            this.selectData();
          }
        },
        {
          type: 'default',
          text: '重置',
          click: () => {
            this.filters.map((item) => {
              item.value = '';
            });
            this.selectData();
          }
        }
      ],
      // 表头查询条件
      parms: {},
      // 查询方法名称
      selectMethod: '',
      // 删除方法名称
      delMethod: '',
      // 模板下载方法名称
      downloadMethod: ''
    }
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await Backstage[this.selectMethod]({
        pageNum: this.pageData.page,
        pageSize: this.pageData.pageSize,
        ...this.parms
      });
      this.tableData.data = res.records;
      this.pageData.total = res.total;
    },
    selectData() {
      this.filters.map((item) => {
        this.parms[item.prop] = item.value;
      });
      this.pageData.page = 1;
      this.getList();
    },
    paginationPage(e) {
      this.getList();
    },
    refreshList() {
      this.pageData.page = 1;
      this.getList();
    },
    handelDel(row) {
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await Backstage[this.delMethod]({ id: row.id });
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        })
        .catch((err) => {
          console.log(err);
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    // 模板下载
    async download() {
      await Backstage[this.downloadMethod]();
    }
  }
}
